package com.jamie.jdk8.lambda.cart;

import com.alibaba.fastjson.JSON;
import org.junit.Test;

import java.util.List;

/**
 * 购物车测试类
 * @author jamie
 */
public class CartServiceTest {

    /** 最基础的判断 找出购物车中所有电子产品 */
    @Test
    public void filterElectronicsSkus() {
        List<Sku> cartSkuList = CartService.getCartSkuList();
        // 查找购物车中数码类商品
        List<Sku> skus = CartService.filterElectronicsSkus(cartSkuList);
        System.out.println(JSON.toJSONString(skus, true));
    }

    /** 根据传入商品类型参数，找出购物车中同种商品类型的商品列表 */
    @Test
    public void filterSkusByCategory() {
        List<Sku> cartSkuList = CartService.getCartSkuList();
        // 查找购物车中图书类商品集合
        List<Sku> skus = CartService.filterSkusByCategory(cartSkuList, SkuCategoryEnum.BOOKS);
        System.out.println(JSON.toJSONString(skus, true));
    }

    /** 支持通过商品类型或总结来过滤商品 */
    @Test
    public void filterSkus3() {
        List<Sku> cartSkuList = CartService.getCartSkuList();
        // 查找购物车中图书类商品集合
        List<Sku> skus = CartService.filterSkus(cartSkuList, SkuCategoryEnum.BOOKS, 0.00, true);
        System.out.println(JSON.toJSONString(skus, true));
        List<Sku> skus2 = CartService.filterSkus(cartSkuList, SkuCategoryEnum.BOOKS, 2699.00, false);
        System.out.println(JSON.toJSONString(skus2, true));
    }

    /** 根据不同的Sku判断标准，对Sku列表进行过滤,用外部接口实现类 */
    @Test
    public void filterSkus4() {
        List<Sku> cartSkuList = CartService.getCartSkuList();
        // 查找购物车中图书类商品集合
        List<Sku> totalPrice = CartService.filterSkus(cartSkuList, new SkuTotalPricePredicate());
        System.out.println(JSON.toJSONString(totalPrice, true));
        List<Sku> book = CartService.filterSkus(cartSkuList, new SkuBooksCategoryPredicate());
        System.out.println(JSON.toJSONString(book, true));
    }

    /** 根据不同的Sku判断标准，对Sku列表进行过滤,用lambda表达式简化调用 */
    @Test
    public void filterSkus5() {
        List<Sku> cartSkuList = CartService.getCartSkuList();
        // 查找购物车中图书类商品集合
        List<Sku> totalPrice = CartService.filterSkus(cartSkuList, sku -> sku.getTotalPrice() > 2000);
        System.out.println(JSON.toJSONString(totalPrice, true));
        List<Sku> book = CartService.filterSkus(cartSkuList, sku -> SkuCategoryEnum.BOOKS.equals(sku.getSkuCategory()));
        System.out.println(JSON.toJSONString(book, true));
    }
}

